package connection;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ServerConnectionConfig implements Serializable {

	/**
	 * 
	 */
	private ServerType serverType;
	private String userName;
	private String password;
	
	private static final long serialVersionUID = 1L;
	public static ServerConnectionConfig deserialize(byte[] source) {
		ByteArrayInputStream stream = new ByteArrayInputStream(source);
		ServerConnectionConfig config = new ServerConnectionConfig();
		// stream.read(config.serverType);

		return config;
	}


	public ServerConnectionConfig() {

	}

	public ServerConnectionConfig(String password, ServerType serverType,
			String userName) {
		super();
		this.password = password;
		this.serverType = serverType;
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public ServerType getServerType() {
		return serverType;
	}

	public String getUserName() {
		return userName;
	}

	public byte[] serialize() {

		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		try {
			stream.write(serverType.ordinal());
			stream.write(userName.getBytes());
			stream.write(password.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

		return stream.toByteArray();
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setServerType(ServerType serverType) {
		this.serverType = serverType;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof ServerConnectionConfig) {
			ServerConnectionConfig config = (ServerConnectionConfig) obj;
			if (serverType != config.getServerType())
				return false;
			if (userName == null)
				return config.getUserName() == null;
			return userName.equals(config.getUserName());
		}
		return false;
	}


	public void save() {
		try {
			File f=new File(".zdbrigTalk");
			if (!f.exists())
				f.mkdir();	
				FileWriter fw = new FileWriter(".zdbrigTalk/"+this.serverType.name()+"_"+userName+".srv");
				PrintWriter pw = new PrintWriter(fw);
				pw.println(serverType.name());
				pw.println(userName);
				pw.println(password);
				pw.close();
				fw.close();

			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	public static List<ServerConnectionConfig> loadAllConfig() {
		List<ServerConnectionConfig> configs = new ArrayList<ServerConnectionConfig>();
		File file = new File(".zdbrigTalk");
		if (!file.exists())
			return null;
		for (File f : file.listFiles(new FileFilter() {

			@Override
			public boolean accept(File pathname) {
				
				return pathname.getName().endsWith("srv");
			}
			
		}))
		{
			try {
				FileReader fr = new FileReader(f);
				BufferedReader br = new BufferedReader(fr);
				ServerConnectionConfig config = new ServerConnectionConfig();
				config.setServerType(ServerType.valueOf(br.readLine()));
				config.setUserName(br.readLine());
				config.setPassword(br.readLine());
				configs.add(config);
				br.close();
				fr.close();
			}
			catch (Exception e) {
				e.printStackTrace();
				f.delete();
			}
		}
		return configs;
	}

}
